//
//  GYVisitorView.swift
//  Swift-sina
//
//  Created by gaoyanyun on 16/5/14.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

//访客视图协议
protocol  VisitorViewDelegate: NSObjectProtocol{
    //访客视图点击注册按钮
    func visitorViewDidRegistor()
    
    //访客视图点击登录按钮
    func visitorViewDidLogin()
    
}
//访客视图
class GYVisitorView: UIView {
    
    //代理属性 代理必须写weak
   weak var delegate:VisitorViewDelegate?
    
    

    //生命周期
    override init (frame: CGRect){
        print(#function)
        
        super.init(frame: frame)
        
        setupUI()
        
        //添加监听方法
        registerButton.addTarget(self, action: #selector(clickRegistorButton), forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: #selector(clickLoginButton), forControlEvents: .TouchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        print("访客视图 deinit")
    }
    
    //监听方法
    @objc private func clickRegistorButton(){
        delegate?.visitorViewDidRegistor()
    }
    @objc private func clickLoginButton(){
        delegate?.visitorViewDidLogin()
    }
    //开始动画
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    //设置访客视图信息
    func setupInfo(imageName: String? = nil, message: String){
     messageLabel.text = message
        guard let imageName = imageName else{
            
            startAnimation()
            
            return
        }
        homeIconView.hidden = true
        maskIconView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    
    
    //遮盖图片
    private lazy var maskIconView: UIImageView = UIImageView(cz_imageName: "visitordiscover_feed_mask_smallicon")
    
    //私有控件
    //图标控件
    private lazy var  iconView :UIImageView = UIImageView (cz_imageName:"visitordiscover_feed_image_smallicon")
    
    //小房子控件
    private lazy var homeIconView:UIImageView = UIImageView(cz_imageName:"visitordiscover_feed_image_house")
    
    //描述文字
    private lazy var messageLabel : UILabel = UILabel(cz_text:"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",alignment:.Center)
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        return button
    }()
    

}

//设置界面
extension GYVisitorView{
    
    private func setupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        messageLabel.textAlignment = .Center
        
        
        //图标
        iconView.snp_makeConstraints{(make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
            
    }
        //小房子
        homeIconView.snp_makeConstraints{ (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        //文字描述
        messageLabel.snp_makeConstraints{ (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(12)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        
        //注册
        registerButton.snp_makeConstraints{ (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(12)
            make.size.equalTo(CGSize(width: 100 ,height: 36))
        }
        //登录
        loginButton.snp_makeConstraints{ (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        //遮盖视图
        maskIconView.snp_makeConstraints{ (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        //空白区域的背景色
        backgroundColor = UIColor (white: 237.0 / 255.0 ,alpha:1.0)
}




}